//app.js

var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
var util = require('./utils/util.js');

App({
  onLaunch: function () {

    if (wx.canIUse('getUpdateManager')){
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)//res.hasUpdate返回boolean类型
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启当前应用？',
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用applyUpdate应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          // 新版本下载失败时执行
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '发现新版本',
              content: '请删除当前小程序，重新搜索打开...',
            })
          })
        }
      })
    }else{
      //如果小程序需要在最新的微信版本体验，如下提示
      wx.showModal({
        title: '更新提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    //重写分享方法
    wx.onAppRoute(function (res) {
      //获取加载的页面
      let pages = getCurrentPages(),
        //获取当前页面的对象
        view = pages[pages.length - 1],
        data;
      if (view) {
        data = view.data;
        console.log('是否重写分享方法', data.isOverShare);
        if (!data.isOverShare) {
          data.isOverShare = true;
          view.onShareAppMessage = function () {
            //分享配置
            return {
              title: '医人才，连接医疗的未来', // 子页面的title
              // path: 'pages/index/index',
              // imageUrl: '/images/logo.png'
            };
          }
        }
      }
    })
    qcloud.setLoginUrl(config.service.loginUrl);
  },
  
  globalData: {
    //设备信息
    systemInfo: null,
    //微信用户唯一id
    wxCode: wx.getStorageSync('wxcode') || null,
    openid: wx.getStorageSync('openid') || null,
    unionid: wx.getStorageSync('unionid') || null,
    phone: wx.getStorageSync('phone') || null,
    sessionkey: wx.getStorageSync('sessionkey') || null,
    //微信用户信息
    userInfo: wx.getStorageSync('userInfo') || null,
    //判断用户是否已有资料
    isHaveResume: wx.getStorageSync('isHaveResume') || null,
    //用于职位详情绑定数据
    positionDetail: null,
    //  用户登录状态
    token: false,
    //用于保存推送码
    globalFormIds: wx.getStorageSync("globalFormIds") || null
  },
  api: {
    getJob: "index/getJobByHot",
    getList: "index/serachJobByCandidate",
    getPositionDetail: "index/jobdetail",
    addRecommend: "index/Recommend",
    deliveryResume: "index/recommendme",
    login: "index/wxlogin",
    posilogin: "index/wxposilogin",
    getUserInfo: "index/getUserInfo",
    toWebOauthTest: "index/toWebOauth",
    isExistRec: "index/isExistRecommendself",
    isHaveResume: "index/isHaveResume",
    myrecommend: "index/myrecommend",
    myInterviewRecord: "user/getInterviewRecord",
    resumeBaseInfo: "index/resumeBaseInfo",
    resumeDomain: "index/resumeDomain",
    resumeNowPosi: "index/resumeNowPosi",
    resumeDreamPosi: "index/resumeDreamPosi",
    sendCode: "index/sendCode",
    do_login: "index/bindePhone",
    similarPosi: "index/similarPosi",
    getAdcarouse: "index/getAdcarouse",
    getArticleDetail: "index/getArticleDetail",
    getTopJobByHot: "index/getTopJobByHot",
    getTopHunter: "index/getTopHunter",
    getAdContent: "index/getAdContent",
    getChannel: "index/getChannel",
    getArticle: "index/getArticle",
    getTopArticleByHot: "index/getYrcTopArticleByHot",
    createUserInfo: "index/createUserInfo",
    updatePosiStatus: "index/updatePosiStatus",
    deliverflow: "index/deliverflow",
    companyTagsPosi: "index/companyTagsPosi",
    highSalaryPosi: "index/highSalaryPosi",
    getHunterList: "new/getHunterList",
    getHunterDetail: "new/hunterdetail",
    getOrderDescList: "new/getOrderDescList",
    getUserCouponList: "new/getUserCoupon",
    getOrderDescId: "new/getOrderDescId",
    createEvaluate: "new/createEvaluate",
    selectByEvaId: "new/selectByEvaId",
    replyEvaluate: "new/replyEvaluate",
    myassess: "new/myassess",
    selectByArticle: "new/selectByArticle",
    getHunterArticles: "new/getHunterArticles",
    getResume: "user/getResume",
    getjobtitle: "user/getjobtitle",
    getJobByTypes: "user/getJobByTypes",
    getcity: "user/getcity",
    getdomain: "user/getdomain",
    getCompany: "user/getCompany",
    getmeddomain: "user/getmeddomain",
    getdrugdomain: "user/getdrugdomain",
    getSubDomain: "user/getSubDomain",
    saveResumeBase: "user/saveResumeBase",
    saveResumeDomain: "user/saveResumeDomain",
    saveResumeNowPosi: "user/saveResumeNowPosi",
    saveResumeDreamPosi: "user/saveResumeDreamPosi",
    getCompanyByDomain: "user/getCompanyByDomain",
    addPosiOpinion: "user/addPosiOpinion",
    empDomain: "user/getCompanyByEmpDomain",
    getFunction: "user/getFunction",
    getPhoneNumber: "user/getPhoneNumber",
    wxpay: "pay/prepay",
    addOrder: "pay/addOrder",
    getUnOrderByStatus: "pay/getUnOrderByStatus",
    getOrderingByStatus: "pay/getOrderingByStatus",
    getSafeOrderByStatus: "pay/getSafeOrderByStatus",
    getOrderedByStatus: "pay/getOrderedByStatus",
    getAssessOrderByStatus: "pay/getAssessOrderByStatus",
    myPayDetail: "pay/myPayDetail",
    updateOrder: "pay/updateOrder",
    updatePaystatus: "pay/updatePaystatus",
    createSafesales: "pay/createSafesales",
    mysafed: "pay/mysafed",
    getOrderedById: "pay/getOrderedById",
    getSafedById: "pay/getSafedById",
    saveFormIds: "form/saveFormIds",
    companyList: "company/getCompanyList",
    companyDetail: "company/getCompanyDetail",
    getJobdescByCompany: "company/getJobdescByCompany",
    getCompanyByVideo: "company/getCompanyByVideo",
    getSearchResult: "company/getSearchResult",
    searchPosi: "index/searchPosi",
    searchCompany: "company/searchCompany",
    isExitResume: "index/isExitResume",
    wechatQrcode:'wechat/qrcode',
    getBackground:'wechat/getBackground',
    //基本信息
    getCompanys: "index/getCompanys",
    getJobs: "index/getJobs",
    getCities: "index/getCities",
    getDomainList: "index/getDomainList",
    checkPhone: "user/checkPhone",
    getByUnionid: "user/getByUnionid",
    userSave: "user/save",

    getDomains: "index/getDomains",

    getResumeList: "user/getResumeList",
    uploadResume: "user/uploadResume",
    deleResume: "user/deleResume",
    updateRecommendStatus: 'user/updateRecommendStatus',

    getPublicityJobByParam: 'index/listJobByParam',

    //校招相关
    getSchPersonByUnionid: 'schPerson/getByUnionid',
    addSchPersonBase: 'schPerson/addBase',
    getSchPersonById: 'schPerson/get',
    updateSchPersonBase: 'schPerson/updateBase',
    updateSchPersonExcept: 'schPerson/updateExcept',
    uploadFile2Qiniu: 'schPerson/uploadFile',
    getSchJobs: 'schJob/page',
    getSchJobById: 'schJob/get',
    hasSendEmail: 'schPerson/hasSendEmail',//判断是否投递，24h内不能重复投递
    sendEmail: 'schPerson/sendEmail',//投递接口
    getSchDic: 'schJob/getDic',
    getSchCompanys: 'schJob/getcompanys2',
    //banner
    getBannerPicture:'index/getFirstBanner',
    getTop5Companys:'company/getIndexCompanies'
  },
  alert: function (msg) {
    if (msg == null) {
      msg = "消息为空";
    }
    wx.showModal({
      content: msg,
      showCancel: false,
    });
  },
  loading: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  },
  hideloading: function () {
    wx.hideLoading();
  },
  getOpenId: function () {
    return wx.getStorageSync('openid');
  },
  getWxCode: function () {
    return wx.getStorageSync('wxcode');
  },
  getUserInfo: function () {
    return wx.getStorageSync('userInfo');
  },
  getSessionKey: function () {
    return wx.getStorageSync('sessionkey');
  },
  hasLogin: function () {
    let openid = this.globalData.openid
    if (openid == null || openid == undefined || openid =="" || this.globalData.userInfo == null) {
      return false;
    }
    return true;
  },
  login: function (callback) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var wxcode = res.code;
          wx.getUserInfo({
            success: function (res) {
              util.post(that.api.getUserInfo, {
                code: wxcode,
                iv: res.iv,
                encryptedData: res.encryptedData
              }, function (res) {
                if (res.code != "200") {
                  // wx.redirectTo({
                  //   url:"/pages/authorization/index"
                  // })
                } else {
                  var openId = res.data.openId;
                  var unionid = res.data.unionId;
                  //到这一步所有需要的信息已经获取，在这里同时把openid和userinfo存到Storage中
                  that.globalData.wxCode = wxcode;
                  that.globalData.userInfo = res.data;
                  that.globalData.openid = openId;
                  that.globalData.unionid = unionid;
                  console.log(that.globalData)
                  wx.setStorageSync('userInfo', res.data);
                  wx.setStorageSync('openid', openId);
                  wx.setStorageSync('unionid', unionid);
                  wx.setStorageSync('wxcode', wxcode);
                  callback(openId, unionid, res.data, wxcode);
                }
              });
            },
            fail: function (res) {
              // wx.redirectTo({
              //   url:"/pages/authorization/index"
              // })
              console.log("授权失败");
            }
          });
        }
      },
      fail: function (res) {
        console.log("登录失败");
      }
    });
  }
})